<template>
  <div id="loginFrom">
    <div div style="margin: 20px"></div>
    <el-form label-position="right" label-width="80px" :model="formLabelAlign" :rules="rules" ref="lForm">
      <el-form-item label="用户/商家">
        <el-radio-group v-model="formLabelAlign.categary">
          <el-radio label="用户"></el-radio>
          <el-radio label="商家"></el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="用户名 : " prop="username">
        <el-input v-model.trim="formLabelAlign.username"  @keyup.enter.native="goHome"></el-input>
      </el-form-item>
      <el-form-item label="密码 : " prop="password">
        <el-input v-model.trim="formLabelAlign.password" type="password" show-password @keyup.enter.native="goHome"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button class="btn" el-button type="primary" @click="goHome">登录</el-button>
        <el-button class="goLogin btn" @click="changeLoginStatus">注册</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import qs from 'qs';
export default {
  data() {
    return {
      formLabelAlign: {
        username: '',
        password: '',
        categary: "用户"
      },
      rules: {
        username: [
          { required: true, message: '请输入正确的用户名', trigger: 'change' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: "blur" }
        ],

      }
    };
  },
  props: ["changeLoginStatus"],
  methods: {
    // 登录按钮
    goHome() {
      this.$refs.lForm.validate(async (valid) => {
        if (valid) {
          //验证通过的逻辑
          // 发送请求请求用户数据,跳转到首页
          let res = await this.$http({
            method: "POST",
            url: 'http://10.3.136.136:8888/login',
            data: qs.stringify(this.formLabelAlign),
            headers: { 'content-type': 'application/x-www-form-urlencoded' },
          })
          if (res.status.code !== 200) {
            this.$message({
              message: "用户名或密码错误",
              type: 'warning'
            });
            return
          }
          // console.log(res);
          if (res.isDisable) {
            this.$message({
              message: '该账号已被封禁',
              type: 'warning'
            });
            return
          }
          let obj = {
            username: res.username,
            categary: res.categary,
            userOrder: res.userOrder,
            picUrl: res.picUrl
          }
          this.$store.commit('GETUSER', obj)
          // 这里还需要把订单部分放到订单里
          this.$message({
            message: '登录成功',
            type: 'success'
          });
          this.$router.push('/home')
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
#loginFrom {
  position: absolute;
  top: 45%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 400px;
  height: 100%;

  .btn {
    width: 45%;
  }

  .goLogin {
    position: absolute;
    right: 0;
  }
}
</style>